﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SVAP
{
    public partial class ManageAlgorithmForm : Form
    {
        internal AddAlgorithmForm aaf = null;// form used to add a new algorithm

        private static string strExePath = "";// ".exe"文件所在路径

        public ManageAlgorithmForm()
        {
            InitializeComponent();

            //将该窗口设置为程序的焦点，且不允许改变，直至关闭该窗口
            //SetWindow setWin = new SetWindow(this.Handle);
            //setWin.Star();
        }

        //关闭窗体
        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ManageAlgorithmForm_Load(object sender, EventArgs e)
        {
            strExePath = System.AppDomain.CurrentDomain.BaseDirectory;
            string strConfigFilePath = System.AppDomain.CurrentDomain.BaseDirectory;// set destination path
            strConfigFilePath += @"Config.txt";

            //从文件中读取算法信息，并在列表中显示
            StreamReader sr = new StreamReader(strConfigFilePath, Encoding.Default);
            string sLine = "";
            sLine = sr.ReadLine();
            if (sLine != null && sLine != "")//处理第一条记录
            {
                string[] strSplit = sLine.Split(new char[] { ' ' });
                string strTemp = "";
                //if (strSplit[0] != null && strSplit[1] != null && strSplit[2] != null)
                //    strTemp = "文件名：" + strSplit[0].ToString() + " 算法描述：" + strSplit[1].ToString() + " 使用频率：" + strSplit[2].ToString();
                if (strSplit[0] != null && strSplit[1] != null)
                    strTemp = "文件名：" + strSplit[0].ToString() + " 算法描述：" + strSplit[1].ToString();
                listBox_showAlgorithm.Items.Add(strTemp);
            }
            while (sLine != null)//循环处理其余各条记录
            {
                sLine = sr.ReadLine();
                if (sLine != null && sLine != "")
                {
                    string[] strSplit = sLine.Split(new char[] { ' ' });
                    string strTemp = "文件名：" + strSplit[0].ToString() + " 算法描述：" + strSplit[1].ToString();
                    listBox_showAlgorithm.Items.Add(strTemp);
                }
            }
            sr.Close();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            //打开添加算法的对话框
            if (aaf == null || aaf.IsDisposed)
            {
                aaf = new AddAlgorithmForm();
                aaf.Owner = this;
            }
            aaf.Show();
            aaf.Focus();
        }
        //获取所有选中的item，并将其删除
        private void btn_del_Click(object sender, EventArgs e)
        {
            //
            string strFileName = "";
            string strSelectedItem = listBox_showAlgorithm.SelectedItem.ToString();
            //分词，获得DLL文件名
            string[] strSplit = strSelectedItem.Split(new char[] { ' ' });
            foreach (char s in strSplit[0])
            {
                if ((int)s == 46 || ((int)s >= 65 && (int)s <= 90) || ((int)s >= 97 && (int)s <= 122))
                {
                    strFileName += s.ToString();
                }
            }
            //string strSourcePath = strExePath + @"\Algorithm\" + strSplit[0].ToString();
            string strSourcePath = strExePath + @"\Algorithm\" + strFileName;
            //MessageBox.Show(strFileName);
            File.Delete(strSourcePath);

            //从列表中删除
            string tempCon = listBox_showAlgorithm.SelectedItem.ToString();
            string strDel = "";

            listBox_showAlgorithm.Items.RemoveAt(listBox_showAlgorithm.SelectedIndex);//删除选中的那一行

            //从配置文件中删除记录
            //先读取每一行，存储到list中，清空文件，从list中删除选中的那一行，然后写回文件
            string strConfigFilePath = System.AppDomain.CurrentDomain.BaseDirectory;// set destination path
            strConfigFilePath += @"Config.txt";
            List<string> tempList = new List<string>();
            StreamReader sr = new StreamReader(strConfigFilePath, Encoding.Default);
            string sLine = "";
            //sLine = sr.ReadLine();
            //tempList.Add(sLine);
            while (sLine != null)
            {
                sLine = sr.ReadLine();
                if (sLine != null)
                {
                    tempList.Add(sLine);
                    string[] strSplit1 = sLine.Split(new char[] { ' ' });//从配置中读出来的直接分词，不需要剔除中文
                    //MessageBox.Show(strSplit1[0]);
                    //MessageBox.Show(strFileName);
                    //MessageBox.Show(sLine);
                    if (strSplit1[0] == strFileName)
                    {
                        strDel = sLine;
                        //MessageBox.Show(strSplit[0]);
                        //MessageBox.Show(strDel);
                    }
                }
            }
            sr.Close();
            // 从list中删除要删除的
            tempList.Remove(strDel);

            //清空并写回文件
            FileStream fs = new FileStream(strConfigFilePath, FileMode.Create);//用Create参数起到“清空文件”的作用
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);

            foreach (string str in tempList)
            {
                sw.WriteLine(str);
            }
            sw.Close();
        }
    }
}
